#!/bin/bash
#PATH=$PATH:'/cygdrive/c/mysql/bin/'
MYSQL_OPTIONS='--batch --default-character-set=czech --skip-column-names -u root -pCYbV9hvDfcQNbquvgdHc moodle4vyuka'
if [ -z $1 ]; then
  echo "Pouziti: $0  <adresar>";
  echo "Zkontroluje postupne pro vsechny podadresare s tokeny ukoluu v zadanem";
  echo "adresari miru shody, tzn. ukol kazdeho studenta je porovnan se vsemi";
  exit 1;
fi
BIN=`dirname $0`
if [ -f yap.summary ] ; then rm yap.summary ; fi
if [ -f vysledek.txt ] ; then rm vysledek.txt ; fi
for dir_ukolu in $1/[0-9]*; do
    if [ -d $dir_ukolu ] ; then   #test nutny!U prazdnych adresaru 'for' vraci *
		id_ukolu=${dir_ukolu##*/}
		printf "idukolu=%s " $id_ukolu
		mysql $MYSQL_OPTIONS -e "
			select name from mdl_assignment where id=$id_ukolu;"
		echo '----------------------------------- '
		find $dir_ukolu -name '*.tokens' > yap.submissions
		for token_studenta in $dir_ukolu/*.pasc.tokens; do
			if [ -f $token_studenta ] ; then
				token=${token_studenta##*/}		#odstran cestu
				id_studenta=${token%.pasc.tokens}	#odstran priponu
				printf "id studenta=%s " $id_studenta
				mysql $MYSQL_OPTIONS -e "
  			     select firstname,lastname from mdl_user where id=$id_studenta;"
				echo $token_studenta > yap.newfiles
				$BIN/rkr_gst 2  "sort -n -r +2 -3 yap.summary" 0 0 2>/dev/null|\
					awk -v idautora=$id_studenta -v idass=$id_ukolu -f $BIN/processResults.awk;
				rm yap.summary

			fi
		done
	fi
done
